<?php
/**
 * Created by PhpStorm.
 * User:雨寒
 * Date: 2018/9/11
 * Time: 16:51
 */

namespace app\www\controller;

use think\Session;
use think\Db;
class Login extends Commons
{
    public function login(){
        if (request()->isGet())
        {
            return $this->fetch();
        }
        if(request()->isPost())
        {
            $data=input();
            $mobile=empty($data['mobile']) ? '':$data['mobile'];
            $captcha=empty($data['captcha']) ? '':$data['captcha'];
            $password=empty($data['password']) ? '':$data['password'];
            $data1=array(
                "module"=>'Member',
                "interface"=>'1002',
                "version"=>'1.0.0',
                "data"=> [
                    "mobile"=> $mobile,
                    "captcha"=> $captcha,
                    "password"=>$password
                ]
            );
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];


            if($result_code!=="0000")
            {
                return $this->error($result_info);

            }else{
                $result_data=json_decode($res,true)['result_data'];
                session::set('access_token',$result_data['access_token']);
                session::set('bussiness_id',$result_data['bussiness_id']);
                $this->redirect('Index/index');
//                return $this->success($result_info,url('Index/index'));
            }
        }
    }

        //注册
    public function register(){

        if(request()->isPost())
        {
            $data=input();
            if(empty($data['parent_mobile'])){
                $data['parent_mobile']=0;
            }
            $data1=array(
                "module"=>'Member',
                "interface"=>'1001',
                "version"=>'1.0.0',
                "data"=> [
                    "real_name"=> $data['real_name'],
                    "mobile"=> $data['mobile'],
                    "captcha"=> $data['captcha'],
                    "password"=>$data['password'],
                    "parent_mobile"=>$data['parent_mobile'],
                    "confirmpassword"=> $data['confirmPassword'],
                    "duties_id"=> $data['duties'],
                    "industry_id"=> $data['industry'],
                    "server"=> $data['server'],
                    "abstract"=> $data['abstract'],
                    "bussiness_id"=> $data['bussiness_id']
                    ]
            );
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];
            if($result_code!=="0000")
            {

                return $this->error($result_info);

            }else{
                return $this->success($result_info,url('Login/login'));
            }


        }else{
            /*职位*/
            $duties=Db::name('duties')
                ->where('id','gt',0)
                ->field('id,title')
                ->select();
            /*行业*/
            $industry=Db::name('industry')
                ->where('is_visible','gt',0)
                ->field('id,title')
                ->select();
            $mobile=input('phone');
            $bussiness_id=input('bussiness_id');
            if(!empty($bussiness_id))
            {
                $department=Db::name('department')
                    ->where('bussiness_id',$bussiness_id)
                    ->field('id,title')
                    ->select();
            }else{
                $department=array();
            }

            $this->assign('mobile',$mobile);
            $this->assign('department',$department);
            $this->assign('bussiness_id',$bussiness_id);
            $this->assign('duties',$duties);
            $this->assign('industry',$industry);
            return $this->fetch();
        }

    }
    public function forget(){
        if(request()->isPost())
        {
            $data=input();
            $data1=array(
                "module"=>'Member',
                "interface"=>'1004',
                "version"=>'1.0.0',
                "data"=> [
                    "mobile"=> $data['mobile'],
                    "captcha"=> $data['captcha'],
                    "newpassword"=>$data['newpassword'],
                    "confirmpassword"=> $data['confirmpassword']
                ]
            );
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];
            if($result_code!=="0000")
            {

                return $this->error($result_info);

            }else{
                return $this->success($result_info,url('Login/login'));
            }
        }else{
            return $this->fetch();
        }

    }

    // 发送短信验证码
    public function captcha()
    {
        $mobile=input('mobile');
        if (request()->isAjax())
        {
            $data1=array(
                "module"=>'Utils',
                "interface"=>'1001',
                "version"=>'1.0.0',
                "data"=> [
                    "mobile"=> $mobile
                ]
            );
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];

            if($result_code!=="0000")
            {
                $this->ajaxError($result_info);
            }else{
                $this->ajaxSuccess($result_info);
            }
        }
    }

    public function logout(){
        session(null);
        $this->redirect('Login/login');
    }
}